package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import util.MyTable;
import util.UtilClass;
import view.component.base.BaseComponent;
import view.component.basecomponent.pojo.ComponentPojo;
import control.Supervisorio;
import entities.Action;
import entities.Variable;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class ComponentWindow extends javax.swing.JFrame {
	private JPanel pnlBody;
	private JTabbedPane pnlAbas;
	private JPanel abaGeral;
	private JTextField txtName;
	private JTextPane txtDescricao;
	private JTextField txtTamanho;
	private JTextField txtImagem;
	private JTextField txtPosicao;
	private JLabel lblDescricao;
	private JButton btnCancel;
	private JButton btnOk;
	private JButton btnAddVar;
	private MyTable tblVariaveis;
	private JLabel lblSize;
	private JButton btnExcluir;
	private JScrollPane pnlScroll;
	private JPanel abaVariaveis;
	private JLabel lblPosition;
	private JLabel lblImage;
	private JLabel lblName;
	private JPanel pnlImagem;
	private JPanel pnlButtons;

	private TableModel tblVariaveisModel;
	private BaseComponent currentBaseComponent;

	private Supervisorio supervisorio;

	/**
	 * Auto-generated main method to display this JFrame
	 */

	public ComponentWindow(BaseComponent baseComponent) {
		super();
		this.currentBaseComponent = baseComponent;
		this.supervisorio = Supervisorio.getInstance();

		initGUI();

		this.txtName.setText(currentBaseComponent.getPojo().getName());
		this.txtPosicao.setText("[" + currentBaseComponent.getPojo().getX()
				+ "," + currentBaseComponent.getPojo().getY() + "]");
		this.txtDescricao.setText(currentBaseComponent.getPojo()
				.getDescription());
		this.txtTamanho.setText("[Largura= " + currentBaseComponent.getWidth()
				+ " ; " + "Altura= " + currentBaseComponent.getHeight() + "]");

		supervisorio.setComponentWindow(this);
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("i2M - Componente");
			this.setResizable(false);
			{
				pnlBody = new JPanel();
				BorderLayout pnlBodyLayout = new BorderLayout();
				pnlBody.setLayout(pnlBodyLayout);
				getContentPane().add(pnlBody, BorderLayout.CENTER);
				{
					pnlAbas = new JTabbedPane();
					pnlBody.add(pnlAbas, BorderLayout.CENTER);
					{
						abaGeral = new JPanel();
						pnlAbas.addTab("Geral", null, abaGeral, null);
						abaGeral.setLayout(null);
						{
							pnlImagem = new JPanel();
							abaGeral.add(pnlImagem);
							pnlImagem.setBackground(Color.WHITE);
							pnlImagem.setBounds(12, 12, 97, 96);
							pnlImagem.setLayout(new BorderLayout());
							pnlImagem.setBorder(BorderFactory
									.createEtchedBorder(BevelBorder.LOWERED));
							ImageIcon icon = UtilClass
									.createImageIcon(currentBaseComponent
											.getImgAddress());
							JLabel lbl = new JLabel(icon);
							pnlImagem.add(lbl);
						}
						{
							lblName = new JLabel();
							abaGeral.add(lblName);
							lblName.setText("Nome:");
							lblName.setBounds(121, 15, 58, 14);
						}
						{
							txtName = new JTextField();
							abaGeral.add(txtName);
							txtName.setBounds(191, 12, 184, 21);
							txtName.setSize(184, 21);
						}
						{
							lblImage = new JLabel();
							abaGeral.add(lblImage);
							lblImage.setText("Imagem:");
							lblImage.setBounds(121, 40, 106, 14);
						}
						{
							lblPosition = new JLabel();
							abaGeral.add(lblPosition);
							lblPosition.setText("Posição:");
							lblPosition.setBounds(121, 67, 106, 14);
						}
						{
							lblSize = new JLabel();
							abaGeral.add(lblSize);
							lblSize.setText("Tamanho:");
							lblSize.setBounds(121, 94, 106, 14);
						}
						{
							lblDescricao = new JLabel();
							abaGeral.add(lblDescricao);
							lblDescricao.setText("Descrição:");
							lblDescricao.setBounds(12, 114, 92, 14);
						}
						{
							txtDescricao = new JTextPane();
							abaGeral.add(txtDescricao);
							txtDescricao.setBounds(12, 134, 363, 88);
							txtDescricao.setBorder(BorderFactory
									.createEtchedBorder(BevelBorder.LOWERED));
						}
						{
							txtPosicao = new JTextField();
							abaGeral.add(txtPosicao);
							txtPosicao.setBounds(191, 64, 182, 21);
							txtPosicao.setSize(184, 21);
							txtPosicao.setEditable(false);
						}
						{
							txtImagem = new JTextField();
							abaGeral.add(txtImagem);
							txtImagem.setBounds(191, 37, 182, 21);
							txtImagem.setSize(184, 21);
						}
						{
							txtTamanho = new JTextField();
							abaGeral.add(txtTamanho);
							txtTamanho.setBounds(191, 91, 181, 21);
							txtTamanho.setSize(184, 21);
							txtTamanho.setEditable(false);
						}
					}
					{
						abaVariaveis = new JPanel();
						pnlAbas.addTab("Variáveis", null, abaVariaveis, null);
						abaVariaveis.setLayout(null);
						{
							pnlScroll = new JScrollPane();
							abaVariaveis.add(pnlScroll);
							pnlScroll.setBounds(29, 5, 329, 190);
							{
								tblVariaveisModel = new DefaultTableModel(
										new String[][] {}, new String[] {
												"Variável", "Endereço" });
								tblVariaveis = new MyTable();
								pnlScroll.setViewportView(tblVariaveis);
								tblVariaveis.setModel(tblVariaveisModel);
								tblVariaveis
										.addMouseListener(new MouseAdapter() {
											public void mouseClicked(
													MouseEvent e) {

												if (e.getClickCount() == 2)
													btnEditVarMouseClicked(e);
											}
										});
							}
						}
						{
							btnAddVar = new JButton();
							abaVariaveis.add(btnAddVar);
							btnAddVar.setText("Adicionar");
							btnAddVar.setBounds(123, 201, 112, 21);
							btnAddVar.addMouseListener(new MouseAdapter() {
								public void mouseClicked(MouseEvent e) {
									btnAddVarMouseClicked(e);
								}
							});
						}
						{
							btnExcluir = new JButton();
							abaVariaveis.add(btnExcluir);
							btnExcluir.setText("Excluir");
							btnExcluir.setBounds(246, 201, 112, 21);
							btnExcluir.addMouseListener(new MouseAdapter() {
								public void mouseClicked(MouseEvent e) {
									btnExcludeMouseClicked();
								}
							});
						}
					}
				}
			}
			{
				pnlButtons = new JPanel();
				getContentPane().add(pnlButtons, BorderLayout.SOUTH);
				pnlButtons.setPreferredSize(new java.awt.Dimension(394, 34));
				{
					btnOk = new JButton();
					pnlButtons.add(btnOk);
					btnOk.setText("OK");
					btnOk.setPreferredSize(new java.awt.Dimension(100, 21));
					btnOk.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent e) {
							btnOkMouseClicked();
						}
					});
				}
				{
					btnCancel = new JButton();
					pnlButtons.add(btnCancel);
					btnCancel.setText("Cancelar");
					btnCancel.setPreferredSize(new java.awt.Dimension(100, 21));
					btnCancel.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent e) {
							btnCancelMouseClicked(e);
						}
					});
				}
				this.updateVariablesTbl();
			}
			pack();
			this.setSize(406, 326);
			this.setLocationRelativeTo(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateVariablesTbl() {

		ArrayList<Variable> variables = new ArrayList<Variable>(
				currentBaseComponent.getPojo().getVariables());

		String[][] data = new String[variables.size()][2];

		for (int i = 0; i < variables.size(); i++) {
			Variable variable = variables.get(i);
			data[i][0] = variable.getName();
			data[i][1] = String.valueOf((int) variable.getAddress());
		}

		tblVariaveisModel = new DefaultTableModel(data, new String[] {
				"Variável", "Endereço" });
		pnlScroll.setViewportView(tblVariaveis);
		tblVariaveis.setModel(tblVariaveisModel);

		tblVariaveis.updateUI();
	}

	private void btnAddVarMouseClicked(MouseEvent e) {

		Variable variable = new Variable();

		Collection<Action> actions = new ArrayList<Action>();
		variable.setActions(actions);

		VariableWindow v = new VariableWindow(variable, currentBaseComponent
				.getPojo());
		v.setVisible(true);

	}

	private void btnEditVarMouseClicked(MouseEvent e) {

		int index = tblVariaveis.getSelectedRow();
		Variable variable = currentBaseComponent.getVariables().get(index);

		VariableWindow v = new VariableWindow(variable, null);
		v.setVisible(true);

	}

	private void btnCancelMouseClicked(MouseEvent e) {
		this.dispose();
	}

	private void btnOkMouseClicked() {

		String name = txtName.getText();
		String description = txtDescricao.getText();
		ComponentPojo pojo = currentBaseComponent.getPojo();
		pojo = supervisorio.updateComponent(pojo, name, description);

		if (pojo != null) {
			currentBaseComponent.setPojo(pojo);
			dispose();
		} else
			JOptionPane.showMessageDialog(null, "errou");

		supervisorio.remoteVariableBuildTree();
	}

	private void btnExcludeMouseClicked() {

		int index = tblVariaveis.getSelectedRow();
		Variable variable = currentBaseComponent.getVariables().get(index);

		supervisorio.deleteVariable(variable);

		this.updateVariablesTbl();
	}

}
